﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Collections.Generic;
using WPFExcelReport.Helpers;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXBL
    {
        public DataRow CreateABK(DataRow dwABP, string ITXDBVERSION)
        {
            DataRow dwABK = INTEXDBC.Convert2ABKOPF(null, ITXDBVERSION, false);
            dwABK["ABKAUFART"] = dwABP["ABPAUFART"];
            dwABK["ABKMANDANT"] = dwABP["ABPMANDANT"];
            dwABK["ABKAUFKRS"] = dwABP["ABPAUFKRS"];
            dwABK["ABKAUFLFD"] = dwABP["ABPAUFLFD"];
            dwABK["ABKAEDAT"] = dwABP["ABPNEUDAT"]; ;
            dwABK["ABKKDAUFTNR"] = dwABP["ABPLTVORL"];
            dwABK["ABKARTSB"] = dwABP["ABPNEUBEN"];
            dwABK["ABKKDAUFDAT"] = dwABP["ABPNEUDAT"];
            dwABK["ABKNEUBEN"] = dwABP["ABPNEUBEN"];
            dwABK["ABKNEUDAT"] = dwABP["ABPNEUDAT"];
            return dwABK;
        }
        public DataRow CreateABR(DataRow dwABP, string ITXDBVERSION)
        {
            DataRow dwABPR = INTEXDBC.Convert2ABPR(null, ITXDBVERSION, false);
            dwABPR["ARMANDANT"] = dwABP["ABPMANDANT"];
            dwABPR["ARAUFKRS"] = dwABP["ABPAUFKRS"];
            dwABPR["ARAUFLFD"] = dwABP["ABPAUFLFD"];
            dwABPR["ARAUFPOS"] = dwABP["ABPAUFPOS"];
            dwABPR["ARBESTKZ"] = " ";
            return dwABPR;
        }
        public string GetEXAUFLFD(string ordType = "B", string year = "4", string yarn = "050", string KEY = "ABKOPF")
        {
            string OrdNum = "000000";
            OracleCommand or1;
            DataTable tbLocal;
            DataRow dwLocal = null;
            try
            {

                KEY = Left(KEY.PadRight(8), 8);
                string SDKEY = "00" + KEY + ordType + year;
                yarn = Right("000" + yarn, 3);
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * FROM SDDAT WHERE SDSA='0022' AND SDKEY LIKE '" + SDKEY + "%' AND SDDATEN LIKE '" + yarn + "S%'";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tbLocal = setResult.Tables[0];
                    tbLocal.TableName = "SDDAT";
                    if (tbLocal.Rows.Count > 0)
                    {
                        dwLocal = tbLocal.Rows[0];
                    }
                    else
                    {
                        dwLocal = tbLocal.NewRow();
                        or1.CommandText = "SELECT  SUBSTR(SDDATEN,74,2)  AS MID FROM SDDAT WHERE SDSA='0022' AND SDKEY LIKE '" + SDKEY + "%'";
                        string idx = "  ";
                        int max = 1;
                        setResult = Connect2Proxy(or1);
                        foreach (DataRow row in setResult.Tables[0].Rows)
                        {
                            int x = Convert.ToInt16(row["MID"].ToString());
                            if (x > max)
                                max = x;
                        }
                        idx = Left(max.ToString().PadRight(2), 2);
                        if (idx != "  ")
                        {
                            idx = Right("  " + (Convert.ToInt16(idx) + 1), 2);
                        }
                        if (idx == "  ")
                        {
                            idx = "1 ";
                        }

                        Helper.GetDate(ref date, ref year, ref month, ref day);
                        SDKEY = "00ABKOPF  " + ordType + Helper.Right(year, 1) + "      " + Left(idx.ToString().PadRight(2), 2) + "  00";
                        string SDDATEN = yarn + "S"
                            + "                00000"
                        + yarn
                        + "00000000"
                        + yarn
                        + "99900000"
                        + yarn + "001" + "V  0000000"
                        + date + "  "
                        + idx
                        + "100010000                                                   "
                        + "ITXAUTO "
                        + date
                        + "        "
                        + "00000000"
                        + "0000*";
                        dwLocal["SDKEY"] = SDKEY;
                        dwLocal["SDDATEN"] = SDDATEN;
                        or1 = OracleDB.CreateCommand();
                        or1.CommandText = "INSERT INTO SDDAT SELECT '0022',:SDKEY,:SDDATEN FROM DUAL";
                        or1.Parameters.Add("SDDATEN", OracleType.NVarChar).Value = dwLocal["SDDATEN"].ToString();
                        or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = SDKEY;
                        Connect2Proxy(or1);
                        throw new Exception("生产单号码被重置，请重新进行计划，并验证生产单号码，若有异常，立即通知管理员");
                    }
                }
                if (dwLocal == null)
                    return OrdNum;
                string Current = dwLocal["SDDATEN"].ToString().Substring(47, 6);
                int length = dwLocal["SDDATEN"].ToString().Length;
                SDKEY = dwLocal["SDKEY"].ToString();
                long currentnum = System.Convert.ToInt64(Current);
                OrdNum = Right("000000" + (currentnum + 1).ToString(), 6);
                dwLocal["SDDATEN"] = dwLocal["SDDATEN"].ToString().Substring(0, 47) + OrdNum + dwLocal["SDDATEN"].ToString().Substring(53, length - 53);
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "UPDATE SDDAT SET SDDATEN=:SDDATEN WHERE SDSA='0022' AND SDKEY=:SDKEY";
                or1.Parameters.Add("SDDATEN", OracleType.NVarChar).Value = dwLocal["SDDATEN"].ToString();
                or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = SDKEY;
                Connect2Proxy(or1);
                DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["ABPMANDANT"] = "00";
                dwData["ABPAUFKRS"] = ordType + year;
                dwData["ABPAUFLFD"] = OrdNum;
                setResult = INTEXDBC.QueryABPOS(dwData, 29);
                if (setResult.Tables[0].Rows.Count > 0)
                {
                    throw new Exception("生产单号码已经存在，请重新进行计划，并验证生产单号码，若有异常，立即通知管理员");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return OrdNum;
        }
        private string GetABP(string AUFTYP, string KRS, ref DataSet db, DataRow dwABP)
        {

            string LFD = "";
            DataSet SResult = new DataSet();
            string filter = "";
            DataRow[] rows;
            DataRow dwABK = null;
            DataTable tbMU = db.Tables["ICC_ABAX"];
            DataTable tbZU = db.Tables["ZUTEI"];
            MANDANT = dwABP["ABPMANDANT"].ToString();
            LFD = dwABP["ABPAUFLFD"].ToString();
            if (LFD.Trim() == "X")
            {
                LFD = GetAUFLFD(KRS, "ABKOPF");
            }

            if (tbMU != null)
            {
                filter = "ABAAUFTYP='V' AND ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + dwABP["ABPAUFLFD"].ToString() + "'";
                rows = tbMU.Select(filter);
                foreach (DataRow row in rows)
                {
                    row["ABAAUFLFD"] = LFD;
                    row["ABAAUFPOS"] = Right("000" + row["ABAAUFPOS"].ToString(), 3);
                }
                filter = "ABAVKAUFTYP='V' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + dwABP["ABPAUFLFD"].ToString() + "'";
                rows = tbMU.Select(filter);
                foreach (DataRow row in rows)
                {
                    row["ABAVKAUFLFD"] = LFD;
                    row["ABAVKAUFPOS"] = Right("000" + row["ABAVKAUFPOS"].ToString(), 3);
                }
            }
            if (tbZU != null)
            {
                filter = "ZUAUFKRS='" + KRS + "' AND ZUAUFLFD='" + dwABP["ABPAUFLFD"].ToString() + "'";
                rows = tbZU.Select(filter);
                foreach (DataRow row in rows)
                {
                    row["ZUAUFLFD"] = LFD;
                    row["ZUAUFPOS"] = Right("000" + row["ZUAUFPOS"].ToString(), 3);
                }
            }
            dwABP["ABPAUFLFD"] = LFD;
            return LFD;
        }
        public void NewReservaton(DataRow dwMat, DataRow dwReq, string ITXDBVERSION, string AUFTYP, ref List<DataRow> list, int ABAAPOS = 2)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwMU = INTEXDBC.Convert2ABAUFM(null, ITXDBVERSION, false); 

            dwMU["ABAMANDANT"] = Helper.Left(dwReq["ABAMANDANT"].ToString().PadRight(2), 2);
            dwMU["ABAAUFTYP"] = "V";
            dwMU["ABAAUFKRS"] = Helper.Left(dwReq["ABAAUFKRS"].ToString().PadRight(2), 2);
            dwMU["ABAAUFLFD"] = Helper.Left(dwReq["ABAAUFLFD"].ToString().PadRight(6), 6);
            dwMU["ABAAUFPOS"] = Helper.Left(dwReq["ABAAUFPOS"].ToString().PadRight(3), 3);
            dwMU["ABAAPOS"] = Helper.Right("000" + ABAAPOS, 4);
            dwMU["ABAVKAUFTYP"] = Helper.Left(dwReq["ABAVKAUFTYP"].ToString().PadRight(1), 1);
            dwMU["ABAVKAUFKRS"] = Helper.Left(dwReq["ABAVKAUFKRS"].ToString().PadRight(2), 2);
            dwMU["ABAVKAUFLFD"] = Helper.Left(dwReq["ABAVKAUFLFD"].ToString().PadRight(6), 6);
            dwMU["ABAVKAUFPOS"] = Helper.Left(dwReq["ABAVKAUFPOS"].ToString().PadRight(3), 3);
            dwMU["ABAVKUPOS"] = Helper.Left(dwReq["ABAVKUPOS"].ToString().PadRight(2), 2);
            dwMU["ABARESMG"] = Convert.ToDouble(dwReq["ABARESMG"].ToString());
            dwMU["ABANEUBEN"] = Helper.Left(dwReq["ABAUSER"].ToString().PadRight(8), 8);
            dwMU["ABANEUDAT"] = Helper.Left(dwReq["ABADATE"].ToString().PadRight(8), 8);
            dwMU["ABARESDAT"] = Helper.Left(dwReq["ABAVKENDD"].ToString().PadRight(8), 8);
            dwMU["ABARESPROG"] = Helper.Left(dwReq["QTYENDDES"].ToString().PadRight(8), 8);  

            dwMU["ABAZUPRIO"] = 0;

            switch (AUFTYP)
            {
                case "AB50MU01":
                    #region====AB50MU01======
                    dwMU["ABASANZSTK"] = Convert.ToDouble(dwReq["ABASANZSTK"].ToString());
                    dwMU["ABASSTKGEW"] = Convert.ToDouble(dwReq["ABASSTKGEW"].ToString());
                    dwMU["ABAAUFMSL"] = Helper.Left(dwReq["ABAAUFMSL"].ToString().PadRight(2), 2);
                    dwMU["ABAPACKTYP"] = Helper.Left(dwReq["ABAPACKTYP"].ToString().PadRight(4), 4);
                    dwMU["ABAHUELSE"] = Helper.Left(dwReq["ABAHUELSE"].ToString().PadRight(4), 4);
                    dwMU["ABAVKART"] = Helper.Left(dwReq["ABAVKART"].ToString().PadRight(24), 24);
                    dwMU["ABAVKFARBE"] = Helper.Left(dwReq["ABAVKFARBE"].ToString().PadRight(10), 10);
                    dwMU["ABAKDPARTIE"] = Helper.Left(dwReq["ABACOLORREF"].ToString().PadRight(10), 10);
                    dwMU["ABASGESLG"] = 0;
                    dwMU["ABASGESGEW"] = 0;
                    dwMU["ABARSTSLG"] = 0;
                    dwMU["ABARSTSGEW"] = 0;
                    dwMU["ABALIEFKOMP"] = "N";
                    dwMU["ABAASSCHNIT"] = " ";
                    #endregion
                    break;
                case "AB05MU01"://依据不同订单，设定不同的默认值，在进行初始计划的时候

                    #region====AB05MU01======
                    dwMU["ABASANZSTK"] = dwReq["ABASANZSTK"];
                    dwMU["ABASSTKGEW"] = dwReq["ABASSTKGEW"];
                    dwMU["ABASSTKLG"] = dwReq["ABASSTKLG"];
                    dwMU["ABASSTKLGV"] = "100";
                    dwMU["ABASSTKLGB"] = "9999000";

                    dwMU["ABAPRAUFKRS"] = dwReq["ABAAUFKRS"];
                    dwMU["ABAPRAUFLFD"] = dwReq["ABAAUFLFD"];
                    dwMU["ABAPRAUFPOS"] = dwReq["ABAAUFPOS"];
                    dwMU["ABAWOBST"] = "I";
                    dwMU["ABAASSCHNIT"] = " ";
                    dwMU["ABAVB"] = dwReq["ABAVB"];
                    dwMU["ABAVG"] = dwReq["ABAVG"];
                    dwMU["ABAVERSDAT"] = dwReq["ABAVKSTARTD"];

                    dwMU["ABARVSACHB"] = dwReq["ABARVSACHB"];
                    dwMU["ABARKUNDNR"] = dwReq["ABARKUNDNR"];
                    dwMU["ABAVLVB"] = dwReq["ABAVLVB"];
                    dwMU["ABAVLVG"] = dwReq["ABAVLVG"];
                    dwMU["ABAVLSACHB"] = dwReq["ABAVLSACHB"];
                    dwMU["ABAPRKUNDNR"] = dwReq["ABARKUNDNR"];
                    dwMU["ABAASSCHNIT"] = " ";
                    dwMU["ABASGESLG"] = 0;
                    dwMU["ABASGESGEW"] = 0;
                    dwMU["ABARSTSLG"] = 0;
                    dwMU["ABARSTSGEW"] = 0;
                    dwMU["ABALIEFKOMP"] = " ";
                    #endregion
                    break;
                case "AB55MU01"://依据不同订单，设定不同的默认值，在进行初始计划的时候
                    #region====AB55MU01======
                    dwMU["ABASANZSTK"] = dwReq["ABASANZSTK"];
                    dwMU["ABASSTKGEW"] = dwReq["ABASSTKGEW"];
                    dwMU["ABASSTKLG"] = dwReq["ABASSTKLG"];
                    dwMU["ABASSTKLGV"] = "100";
                    dwMU["ABASSTKLGB"] = "9999000";
                    dwMU["ABAZUPRIO"] = 8;
                    dwMU["ABAWOBST"] = "I";
                    dwMU["ABAASSCHNIT"] = " ";
                    dwMU["ABASGESLG"] = 0;
                    dwMU["ABASGESGEW"] = 0;
                    dwMU["ABARSTSLG"] = 0;
                    dwMU["ABARSTSGEW"] = 0;
                    dwMU["ABALIEFKOMP"] = " ";
                    #endregion
                    break;
                case "AB45MU01"://依据不同订单，设定不同的默认值，在进行初始计划的时候
                    #region====AB45MU01======
                    dwMU["ABASANZSTK"] = dwReq["ABASANZSTK"];
                    dwMU["ABASSTKGEW"] = dwReq["ABASSTKGEW"];
                    dwMU["ABASSTKLG"] = dwReq["ABASSTKLG"];
                    dwMU["ABASSTKLGV"] = "100";
                    dwMU["ABASSTKLGB"] = "9999000";
                    dwMU["ABAZUPRIO"] = 8;
                    dwMU["ABAWOBST"] = " ";
                    dwMU["ABAASSCHNIT"] = " ";
                    dwMU["ABASGESLG"] = 0;
                    dwMU["ABASGESGEW"] = 0;
                    dwMU["ABARSTSLG"] = 0;
                    dwMU["ABARSTSGEW"] = 0;
                    dwMU["ABALIEFKOMP"] = " ";
                    dwMU["ABAASSCHNIT"] = " ";
                    #endregion
                    break;
                default:
                    break;
            }

            list.Add(dwMU);

        }
        public void ConsoldateFlow(DataRow dwABP, string ITXDBVERSION, string AUFTYP, ref List<DataRow> list)
        {
            DataSet STemp = null;
            DataTable tbAPF = null;
            DataRow dwABF = null;
            DataSet SResult = new DataSet();
            string KRS = "", LFD = "", POS = "", ABPNEUBEN, ABPNEUDAT;
            string filter = "";
            DataRow[] rows;
            string ABPANRKR = dwABP["ABPANRKR"].ToString();
            string ABPALFDNR = dwABP["ABPALFDNR"].ToString();
            decimal ABPSOLLMENGE5 = Convert.ToDecimal(dwABP["ABPSOLLMENGE5"].ToString());
            decimal ABPSOLLMENGE2 = Convert.ToDecimal(dwABP["ABPSOLLMENGE2"].ToString());
            decimal ABPSOLLMENGE1 = Convert.ToDecimal(dwABP["ABPSOLLMENGE1"].ToString());
            decimal ABPSOLLMENGE8 = Convert.ToDecimal(dwABP["ABPSOLLMENGE8"].ToString());
            MANDANT = dwABP["ABPMANDANT"].ToString();
            KRS = dwABP["ABPAUFKRS"].ToString();
            LFD = dwABP["ABPAUFLFD"].ToString();
            POS = dwABP["ABPAUFPOS"].ToString();
            ABPNEUBEN = dwABP["ABPNEUBEN"].ToString();
            ABPNEUDAT = dwABP["ABPNEUDAT"].ToString();
            string ABPSTARTDAT = dwABP["ABPSTARTDAT"].ToString();
            string ABPTERMDAT = dwABP["ABPTERMDAT"].ToString();

            DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["APFMANDANT"] = dwABP["ABPMANDANT"].ToString();
            dwData["APFANRKRS"] = ABPANRKR;
            dwData["APFALFDNR"] = ABPALFDNR;
            SResult = INTEXDBC.QueryAPFOLGE(dwData, 5);
            tbAPF = SResult.Tables[0];
            int nstep = 0;
            foreach (DataRow dwAPF in tbAPF.Rows)
            {
                string step = dwAPF["APFAFOLGE"].ToString();
                dwData["APAKMANDANT"] = MANDANT;

                nstep++;
                #region====dwABF======
                dwABF = INTEXDBC.Convert2ABFOLGE(null, ITXDBVERSION, false);
                dwABF["ABFMANDANT"] = MANDANT;
                dwABF["ABFAUFKRS"] = KRS;
                dwABF["ABFAUFLFD"] = LFD;
                dwABF["ABFAUFPOS"] = POS;
                dwABF["ABFAFOLGE"] = Right("000" + nstep * 10, 3);
                dwABF["ABFNEUBEN"] = ABPNEUBEN;
                dwABF["ABFNEUDAT"] = ABPNEUDAT;
                dwABF["ABFKSTKRS"] = dwAPF["APFKSTKRS"];
                dwABF["ABFKST"] = dwAPF["APFKST"];
                dwABF["ABFAGANG"] = dwAPF["APFAGANG"];
                dwABF["ABFBZAGANG"] = dwAPF["APFBZAGANG"];
                dwABF["ABFLIEFNR"] = dwAPF["KSZLIEFNR"];
                dwABF["ABFMANUELKZ"] = dwAPF["KSZMANUELKZ"];
                dwABF["ABFPARALKZ"] = dwAPF["APFPARALKZ"];
                dwABF["ABFSTUFE"] = dwABP["ABPWAKZ"];

                dwABF["ABFMTYP"] = dwAPF["APFMTYP"];
                dwABF["ABFKENNUNG"] = dwAPF["APFKENNUNG"];
                dwABF["ABFRUECKKZ"] = dwAPF["APFRUECKKZ"];
                dwABF["ABFZUSATZ1"] = dwAPF["APFZUSATZ1"];
                dwABF["ABFZUSATZ2"] = dwAPF["APFZUSATZ2"];
                dwABF["ABFZUSATZ3"] = dwAPF["APFZUSATZ3"];
                dwABF["ABFZUSATZ4"] = dwAPF["APFZUSATZ4"];
                dwABF["ABFZUSATZ5"] = dwAPF["APFZUSATZ5"];
                dwABF["ABFFZUSATZKZ1"] = dwAPF["APFZUSATZ1"];
                dwABF["ABFFZUSATZKZ2"] = dwAPF["APFZUSATZ2"];
                dwABF["ABFFZUSATZKZ3"] = dwAPF["APFZUSATZ3"];
                dwABF["ABFFZUSATZKZ4"] = dwAPF["APFZUSATZ4"];
                dwABF["ABFFZUSATZKZ5"] = dwAPF["APFZUSATZ5"];
                dwABF["ABFFERTIG"] = dwAPF["APFFERTIG"];
                dwABF["ABFMATABR"] = dwAPF["APFMATABR"];
                dwABF["ABFDOMPART"] = dwAPF["APFDOMPART"];
                dwABF["ABFLAGPLKZ"] = dwAPF["APFLAGPLKZ"];

                dwABF["ABFEVARHNR"] = "01";
                dwABF["ABFEVARUNR"] = "01";
                //dwABF["ABFBNRKRS"] = dwAPF["APFBNRKRS"];
                //dwABF["ABFBLFDNR"] =Right(dwAPF["APFBLFDNR"].ToString(),4);
                //dwABF["ABFBPOS"] = dwAPF["APFAFOLGE"];
                dwABF["ABFPROBE"] = dwAPF["APFPROBE"];

                dwABF["ABFTSDAT"] = ABPSTARTDAT;
                dwABF["ABFTSSDAT"] = ABPSTARTDAT;
                dwABF["ABFTESDAT"] = ABPSTARTDAT;
                dwABF["ABFANRKRS"] = ABPANRKR;
                dwABF["ABFALFDNR"] = ABPALFDNR;
                dwABF["ABFLO"] = dwAPF["KSZLO"];
                dwABF["ABFLB"] = dwAPF["KSZLB"];
                dwABF["ABFLP"] = dwAPF["KSZLP"];
                dwABF["ABFME"] = dwAPF["APFME"];
                dwABF["ABFSOLLSTART"] = ABPSTARTDAT;
                dwABF["ABFSOLLDAT"] = ABPTERMDAT;
                dwABF["ABFSOLLSTARTA"] = ABPSTARTDAT;
                dwABF["ABFSOLLDATA"] = ABPTERMDAT;
                dwData["APAKANRKRS"] = ABPANRKR;
                dwData["APAKALFDNR"] = ABPALFDNR;
                dwData["APAKAFOLGE"] = step;

                dwABF["ABFFIX"] = "N";
                dwABF["ABFBASMGE"] = 99999000;
                dwABF["ABFMASCHZT"] = 100;
                dwABF["ABFAGRVON"] = 0;
                dwABF["ABFAGRBIS"] = 0;
                /*
                dwABF["ABFMASCHANZ"] = 10000;
                dwABF["ABFMASCHZE"] = "MI"; 
                dwABF["ABFBNRKRS"] = dwAPF["AMNRKRS"];
                dwABF["ABFBLFDNR"] = dwAPF["AMLFDNR"];
                dwABF["ABFBPOS"] = dwAPF["AMPOS"]; */

                STemp = INTEXDBC.QueryAPARTK(dwData, 2);
                foreach (DataRow dwAPK in STemp.Tables[0].Rows)
                {
                    dwABF["ABFFIX"] = dwAPK["APAKFIX"];
                    dwABF["ABFBASMGE"] = dwAPK["APAKBASMGE"];
                    dwABF["ABFMASCHANZ"] = dwAPK["APAKMASCHANZ"];
                    dwABF["ABFMASCHZE"] = dwAPK["APAKMASCHZE"];
                    dwABF["ABFMASCHZT"] = dwAPK["APAKMASCHZT"];
                    dwABF["ABFAGRVON"] = dwAPK["APAKAGRVON"];
                    dwABF["ABFAGRBIS"] = dwAPK["APAKAGRBIS"];
                    dwABF["ABFBNRKRS"] = dwAPK["AMNRKRS"];
                    dwABF["ABFBLFDNR"] = dwAPK["AMLFDNR"];
                    dwABF["ABFBPOS"] = dwAPK["AMPOS"];
                    break;
                }
                switch (AUFTYP)
                {
                    case "AB05":
                        #region=====AB05===========
                        //dwABF["ABFLIEFNR"] = dwAPF["APFLIEFNR"]; 
                        dwABF["ABFSOLLMG"] = ABPSOLLMENGE1 * 10;
                        dwABF["ABFSOLLSTK"] = ABPSOLLMENGE8 / 100;
                        dwABF["ABFSOLLME"] = 1;
                        dwABF["ABFMEKOPFKZ"] = " ";
                        dwABF["ABFSOLLMTR"] = ABPSOLLMENGE1 * 10;
                        dwABF["ABFBASMGE"] = 0;
                        dwABF["ABFME"] = " ";
                        #endregion
                        break;
                    case "AB55":
                        #region=====AB55===========
                        dwABF["ABFSOLLMG"] = ABPSOLLMENGE2 * 10;
                        dwABF["ABFSOLLSTK"] = ABPSOLLMENGE8 / 100;
                        dwABF["ABFSOLLME"] = 2;
                        dwABF["ABFSOLLMTR"] = ABPSOLLMENGE2 * 10;
                        dwABF["ABFMEKOPFKZ"] = 2;
                        dwABF["ABFBASMGE"] = 99999000;
                        dwABF["ABFME"] = "2";
                        #endregion
                        break;
                    case "AB45":
                        #region=====AB45===========
                        dwABF["ABFSOLLMG"] = ABPSOLLMENGE2 * 10;
                        dwABF["ABFSOLLSTK"] = ABPSOLLMENGE8 / 100;
                        dwABF["ABFSOLLME"] = 2;
                        dwABF["ABFSOLLMTR"] = ABPSOLLMENGE2 * 10;
                        dwABF["ABFMEKOPFKZ"] = 2;
                        dwABF["ABFBASMGE"] = 99999000;
                        dwABF["ABFME"] = "2";
                        #endregion
                        break;
                    case "AB50":
                        #region=====AB50===========
                        dwABF["ABFSOLLMG"] = ABPSOLLMENGE5 * 10;
                        dwABF["ABFSOLLSTK"] = 0;
                        dwABF["ABFSOLLME"] = 5;
                        dwABF["ABFSOLLMTR"] = 0;
                        dwABF["ABFMEKOPFKZ"] = 2;
                        dwABF["ABFME"] = 5;
                        #endregion
                        break;
                    default:
                        break;
                }
                #endregion
                list.Add(dwABF);
                #region====ABARTE======
                dwData["APAKMANDANT"] = MANDANT;
                dwData["APAKANRKRS"] = ABPANRKR;
                dwData["APAKALFDNR"] = ABPALFDNR;
                dwData["APAKAFOLGE"] = step;
                STemp = INTEXDBC.QueryAPARTE(dwData, 2);
                foreach (DataRow rset in STemp.Tables[0].Rows)
                {
                    DataRow dwABK = INTEXDBC.Convert2ABARTE(null, AUFTYP, false);
                    dwABK["ABEMANDANT"] = dwABF["ABFMANDANT"];
                    dwABK["ABEAUFKRS"] = dwABF["ABFAUFKRS"];
                    dwABK["ABEAUFLFD"] = dwABF["ABFAUFLFD"];
                    dwABK["ABEAUFPOS"] = dwABF["ABFAUFPOS"];
                    dwABK["ABEAFOLGE"] = dwABF["ABFAFOLGE"];
                    dwABK["ABEAEVOR"] = rset["APAEAEVOR"];
                    dwABK["ABEMODNR"] = rset["APAEAFOLGE"];
                    dwABK["ABEEINSTNR"] = rset["APAEEINSTNR"];
                    dwABK["ABEWERTX"] = rset["APAEWERTX"];
                    dwABK["ABEWERTN"] = rset["APAEWERTN"];

                    dwABK["ABEPROGNR"] = rset["APAEMODNR"];
                    dwABK["ABEEINAUS"] = rset["APAEEINAUS"];
                    dwABK["ABEREZEPTKZ"] = rset["APAEREZEPTKZ"];
                    dwABK["ABEREZEPTNR"] = rset["APAEREZEPTNR"];
                    dwABK["ABEREZEPTST"] = rset["APAEREZEPTST"];
                    dwABK["ABEWERKZ"] = rset["APAESTAKZ"];
                    dwABK["ABEFAKTOR"] = "00";
                    dwABK["ABESTAKZ"] = rset["APAEREZEPTST"];
                    dwABK["ABEMODEIN"] = rset["APAEMODEIN"];
                    dwABK["ABEIWERTX"] = rset["APAEWERTX"];
                    dwABK["ABEIWERTN"] = rset["APAEWERTN"];
                    dwABK["ABENEUBEN"] = dwABF["ABFNEUBEN"];
                    dwABK["ABENEUDAT"] = dwABF["ABFNEUDAT"];
                    dwABK["ABEPFLKZ"] = " ";
                    list.Add(dwABK);
                }
                #endregion
            }
        }
        public DataSet QueryABK(string MANDANT, string KRS, string LFD)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwKK = null;
            try
            {
                DataRow dwData = null;
                #region=======ABKOPF=======
                dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["ABKMANDANT"] = MANDANT;
                dwData["ABKAUFKRS"] = KRS;
                dwData["ABKAUFLFD"] = LFD;
                Result = INTEXDBC.QueryABKOPF(dwData);
                if (Result.Tables[0].Rows.Count == 0)
                    return Return;
                Return.Merge(Result);
                #endregion
                #region=======ABPOS=======
                dwData["ABPMANDANT"] = MANDANT;
                dwData["ABPAUFKRS"] = KRS;
                dwData["ABPAUFLFD"] = LFD;
                Result = INTEXDBC.QueryABPOS(dwData, 2);
                Return.Merge(Result);
                #endregion
                #region=======ABPR=======
                dwData["ARMANDANT"] = MANDANT;
                dwData["ARAUFKRS"] = KRS;
                dwData["ARAUFLFD"] = LFD;
                Result = INTEXDBC.QueryABPR(dwData, 4);
                Return.Merge(Result);
                #endregion
                #region=======MKRF=======
                dwData["ABPMANDANT"] = MANDANT;
                dwData["ABPAUFKRS"] = KRS;
                dwData["ABPAUFLFD"] = LFD;
                Result = INTEXDBC.QueryMKRF(dwData, 3);
                Return.Merge(Result);
                dwData["ABFMANDANT"] = MANDANT;
                dwData["ABFAUFKRS"] = KRS;
                dwData["ABFAUFLFD"] = LFD;
                Result = INTEXDBC.QueryICC_MKRF(dwData, 5);
                Return.Merge(Result);
                #endregion

                #region=======ABFOLGE=======

                dwData["ABFMANDANT"] = MANDANT;
                dwData["ABFAUFKRS"] = KRS;
                dwData["ABFAUFLFD"] = LFD;
                Result = INTEXDBC.QueryABFOLGE(dwData, 2);
                Return.Merge(Result);
                #endregion
                #region=======ABAUFM=======

                dwData["ABAMANDANT"] = MANDANT;
                dwData["ABAAUFKRS"] = KRS;
                dwData["ABAAUFLFD"] = LFD;
                Result = INTEXDBC.QueryABAUFM(dwData, 4);
                Return.Merge(Result);
                dwData["ABAMANDANT"] = MANDANT;
                dwData["ABAVKAUFKRS"] = KRS;
                dwData["ABAVKAUFLFD"] = LFD;
                Result = INTEXDBC.QueryABAUFM(dwData, 7);
                Return.Merge(Result);
                #endregion

                #region=======WDECK=======

                dwData["WDMANDANT"] = MANDANT;
                dwData["WDAUFKRS"] = KRS;
                dwData["WDAUFLFD"] = LFD;
                Result = INTEXDBC.QueryWDECK(dwData, 21);
                Return.Merge(Result);

                #endregion
                #region=======BAAUF=======

                dwData["BAMANDANT"] = MANDANT;
                dwData["BAAUFKRS"] = KRS;
                dwData["BAAUFLFD"] = LFD;
                Result = INTEXDBC.QueryBAAUF(dwData, 4);
                Return.Merge(Result);

                #endregion
                #region=======BABEW=====
                dwData["BAMANDANT"] = MANDANT;
                dwData["BAAUFKRS"] = KRS;
                dwData["BAAUFLFD"] = LFD;
                Result = INTEXDBC.QueryBABEW(dwData, 2);
                Return.Merge(Result);
                #endregion
                #region===Master=======
                dwData["ABPMANDANT"] = MANDANT;
                dwData["ABPAUFKRS"] = KRS;
                dwData["ABPAUFLFD"] = LFD;
                Result = INTEXDBC.QueryBSSATZ(dwData, 3);
                Return.Merge(Result);

                dwData["ABPMANDANT"] = MANDANT;
                dwData["ABPAUFKRS"] = KRS;
                dwData["ABPAUFLFD"] = LFD;
                Result = INTEXDBC.QueryBHSATZ(dwData, 3);
                Return.Merge(Result);

                dwData["ABPMANDANT"] = MANDANT;
                dwData["ABPAUFKRS"] = KRS;
                dwData["ABPAUFLFD"] = LFD;
                Result = INTEXDBC.QueryWASTAMM(dwData, 8);
                Return.Merge(Result);

                dwData["ABPMANDANT"] = MANDANT;
                dwData["ABPAUFKRS"] = KRS;
                dwData["ABPAUFLFD"] = LFD;
                Result = INTEXDBC.QueryBASTGA(dwData, 5);
                Return.Merge(Result);

                dwData["GAMANDANT"] = MANDANT;
                dwData["GAAUFKRS"] = KRS;
                dwData["GAAUFLFD"] = LFD;
                Result = INTEXDBC.QueryGSDAT(dwData, 4);
                Return.Merge(Result);

                dwData["GAMANDANT"] = MANDANT;
                dwData["GAAUFKRS"] = KRS;
                dwData["GAAUFLFD"] = LFD;
                Result = INTEXDBC.QueryGSCOLOR(dwData, 4);
                Return.Merge(Result);
                #endregion
                #region=======GSMATK=======

                dwData["GAMANDANT"] = MANDANT;
                dwData["GAAUFKRS"] = KRS;
                dwData["GAAUFLFD"] = LFD;
                Result = INTEXDBC.QueryGSMATK(dwData, 4);
                Return.Merge(Result);
                #endregion
                #region=====GAAUF/BAAUF==========
                dwData["ABPMANDANT"] = MANDANT;
                dwData["ABPAUFKRS"] = KRS;
                dwData["ABPAUFLFD"] = LFD;
                Result = INTEXDBC.QueryBAAUF(dwData, 5);
                dwData["ABPMANDANT"] = MANDANT;
                dwData["ABPAUFKRS"] = KRS;
                dwData["ABPAUFLFD"] = LFD;
                Result = INTEXDBC.QueryBABST(dwData, 6);
                Return.Merge(Result);
                dwData["GAMANDANT"] = MANDANT;
                dwData["GAAUFKRS"] = KRS;
                dwData["GAAUFLFD"] = LFD;
                Result = INTEXDBC.QueryGAAUF(dwData, 4);
                Return.Merge(Result);
                #endregion
                #region=======ZUTEI=======

                dwData["ZUMANDANT"] = MANDANT;
                dwData["ZUAUFKRS"] = KRS;
                dwData["ZUAUFLFD"] = LFD;
                Result = INTEXDBC.QueryZUTEI(dwData, 5);
                Return.Merge(Result);
                dwData["ZUMANDANT"] = MANDANT;
                dwData["ZUAUFKRS"] = KRS;
                dwData["ZUAUFLFD"] = LFD;
                Result = INTEXDBC.QueryZUTEI(dwData, 4);
                Result.Tables[0].TableName = "ZUTEI_ITX";
                Return.Merge(Result);
                dwData["ZUMANDANT"] = MANDANT;
                dwData["ZUAUFKRS"] = KRS;
                dwData["ZUAUFLFD"] = LFD;
                Result = INTEXDBC.QueryZUTEI(dwData, 6);
                Result.Tables[0].TableName = "ZUTEI_EXT";
                Return.Merge(Result);
                #endregion
                #region=======recipe=======

                dwData["ARAMANDANT"] = MANDANT;
                dwData["ARAAUFKRS"] = KRS;
                dwData["ARAAUFLFD"] = LFD;
                Result = INTEXDBC.QueryARANS(dwData, 2);
                Return.Merge(Result);

                dwData["ARAMANDANT"] = MANDANT;
                dwData["ARAAUFKRS"] = KRS;
                dwData["ARAAUFLFD"] = LFD;
                Result = INTEXDBC.QueryLRPOS(dwData, 2);
                foreach (DataRow row in Result.Tables[0].Rows)
                {
                    string str = row["LRPKONZ"].ToString();
                    if (str.Trim() == "")
                        str = "0";
                    decimal LRPKONZ = (Convert.ToDecimal(str) / 1000) / 1000;//0008000000
                    str = row["ARAMGKG"].ToString();
                    if (str.Trim() == "")
                        str = "0";
                    decimal ARAMGKG = Convert.ToDecimal(str) / 1000;//90000
                    str = row["ARAFLOTTE"].ToString();
                    if (str.Trim() == "")
                        str = "0";
                    decimal ARAFLOTTE = Convert.ToDecimal(str) / 1000;//800000
                    decimal usage = 0;
                    switch (row["LRPBEZUG"].ToString())
                    {
                        case "A":
                            usage = LRPKONZ * ARAFLOTTE;//gram
                            break;
                        case "C":
                            usage = LRPKONZ * ARAMGKG * 1000;//kg
                            break;
                    }
                    row["LRPDRUCK"] = usage;
                }
                Return.Merge(Result);
                #endregion
                #region======comments========
                foreach (DataRow row in Return.Tables["ABPOS"].Rows)
                {
                    string order = "";
                    string remark = "";
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + " ";
                    dwData["TXTIMANDANT"] = MANDANT;
                    dwData["TXTITXTART"] = "AB3";
                    dwData["TXTISIND"] = Left(order.PadRight(60), 60);
                    SResult = INTEXDBC.QueryTXTKON(dwData, 1);
                    foreach (DataRow dwTemp in SResult.Tables["TXTKON"].Rows)
                    {
                        string str = GetGB2312Value(dwTemp, "TXTKTEXT");
                        //if (str.Trim() != "")
                        //{
                        // remark += str + Environment.NewLine;
                        //}
                    }
                    remark += remark + Environment.NewLine;
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + row["ABPAUFPOS"].ToString();
                    dwData["TXTIMANDANT"] = MANDANT;
                    dwData["TXTISIND"] = Left(order.PadRight(60), 60);
                    dwData["TXTITXTART"] = "ABP";
                    SResult = INTEXDBC.QueryTXTKON(dwData, 1);
                    foreach (DataRow dwTemp in SResult.Tables["TXTKON"].Rows)
                    {
                        string str = GetGB2312Value(dwTemp, "TXTKTEXT");
                        //if (str.Trim() != "")
                        //{
                        // remark += str + Environment.NewLine;
                        //}
                    }
                    row["ABPFILLER1"] = remark;
                }
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
      
        public void DELTEABPOS(string client, string krs, string lfd, string pos = "000")
        {
            try
            {
                DataRow dwData = null;
                /*DataSet SResult = INTEXDBC.QueryABPOS(client, krs, lfd);
                if (!SResult.Tables.Contains("ABPOS"))
                return;
                if (SResult.Tables[0].Rows.Count == 0)
                return;*/
                dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["ABAMANDANT"] = client;
                dwData["ABAVKAUFKRS"] = krs;
                dwData["ABAVKAUFLFD"] = lfd;
                SResult = INTEXDBC.QueryICC_ABAUFM(dwData, 28);
                if (SResult.Tables.Contains("ICC_ABAUFM"))
                {
                    if (SResult.Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show("有采购单或留位，请先取消!");
                        return;
                    }
                }
                switch (pos)
                {
                    case "000":
                        INTEXDBC.DeleteABARTE(client, krs, lfd, pos, 2);//工艺参数
                        INTEXDBC.DeleteABFOLGE(client, krs, lfd, pos, 2);//工艺步骤
                        INTEXDBC.DeleteGAAUF(client, krs, lfd, pos, 2);//纱线需求
                        INTEXDBC.DeleteZUTEI(client, krs, lfd, pos, 2);//箱子级别的留位
                        INTEXDBC.DeleteMDECK(client, krs, lfd, pos, 2);//染化料需求
                        INTEXDBC.DeleteWDECK(client, krs, lfd, pos, "", 2);//面料需求
                        INTEXDBC.DeleteFDECK(client, krs, lfd, pos, "", "", 2);//成衣需求
                        INTEXDBC.DeleteABPOS(client, krs, lfd, pos, 2);//生产单行
                        INTEXDBC.DeleteICC_ABAUFM(client, krs, lfd, pos, null, 2);//自定义的关系表
                        INTEXDBC.DeleteABAUFM(client, krs, lfd, pos, 2);//预留关系，昨天提到
                        INTEXDBC.DeleteABPR(client, krs, lfd, pos, 2);//生产单价格
                        //INTEXDBC.DeleteICC_ABPOSFS(client, krs, lfd, pos); 
                        INTEXDBC.DeleteABKOPF(client, krs, lfd);//生产单头
                        INTEXDBC.DeleteICC_ABPOSFS(client, krs, lfd);//成衣生产用的矩阵
                        INTEXDBC.DeleteICC_ABPOS_MCO(client, krs, lfd, 1);//成衣生产用的成本
                        dwData = INTEXDB.ICC_QUERYTB.NewRow();
                        dwData["KRS"] = krs;
                        dwData["LFD"] = lfd;
                        INTEXDBC.DeleteICC_ABPOS_MAT(dwData, 3);//成衣生产用的原料矩阵
                        dwData["ABAMANDANT"] = client;
                        dwData["ABAAUFTYP"] = "V";
                        dwData["ABAAUFKRS"] = krs;
                        dwData["ABAAUFLFD"] = lfd;
                        INTEXDBC.DeleteICC_ABAX(dwData, 2);//我预留到你
                        dwData["ABAVKAUFKRS"] = krs;
                        dwData["ABAVKAUFLFD"] = lfd;
                        INTEXDBC.DeleteICC_ABAX(dwData, 5);//你预定了我
                        break;
                    default:
                        INTEXDBC.DeleteABARTE(client, krs, lfd, pos, 1);
                        INTEXDBC.DeleteABFOLGE(client, krs, lfd, pos, 1);
                        INTEXDBC.DeleteGAAUF(client, krs, lfd, pos, 1);
                        INTEXDBC.DeleteZUTEI(client, krs, lfd, pos, 1);
                        INTEXDBC.DeleteMDECK(client, krs, lfd, pos, 1);
                        INTEXDBC.DeleteWDECK(client, krs, lfd, pos, "", 1);
                        INTEXDBC.DeleteFDECK(client, krs, lfd, pos, "", "", 1);
                        INTEXDBC.DeleteABPOS(client, krs, lfd, pos, 1);
                        INTEXDBC.DeleteICC_ABAUFM(client, krs, lfd, pos, null, 1);
                        INTEXDBC.DeleteABAUFM(client, krs, lfd, pos, 1);
                        INTEXDBC.DeleteABPR(client, krs, lfd, pos, 1);
                        //INTEXDBC.DeleteICC_ABPOSFS(client, krs, lfd, pos);
                        dwData = INTEXDB.ICC_QUERYTB.NewRow();
                        dwData["ABPMANDANT"] = client;
                        dwData["ABPAUFKRS"] = krs;
                        dwData["ABPAUFLFD"] = lfd;
                        SResult = INTEXDBC.QueryABPOS(dwData, 2);
                        if (SResult.Tables[0].Rows.Count == 0)
                        {
                            INTEXDBC.DeleteABKOPF(client, krs, lfd);
                        }
                        dwData["ABAMANDANT"] = client;
                        dwData["ABAAUFTYP"] = "V";
                        dwData["ABAAUFKRS"] = krs;
                        dwData["ABAAUFLFD"] = lfd;
                        dwData["ABAAUFPOS"] = pos;
                        INTEXDBC.DeleteICC_ABAX(dwData, 3);

                        dwData["ABAVKAUFKRS"] = krs;
                        dwData["ABAVKAUFLFD"] = lfd;
                        dwData["ABAVKAUFPOS"] = pos;
                        INTEXDBC.DeleteICC_ABAX(dwData, 4);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet QueryABKON(string MANDANT, string KRS, string LFD, string POS, string EXART)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwKK = null;
            try
            {
                DataRow dwData = null;

                dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["ABAMANDANT"] = MANDANT;
                dwData["EXART"] = EXART;
                Result = INTEXDBC.QueryICC_ABAX(dwData, 6);
                Return.Merge(Result);
                dwData["ABAMANDANT"] = MANDANT;
                dwData["EXART"] = EXART;
                Result = INTEXDBC.QueryICC_MKRF(dwData, 7);
                Return.Merge(Result);
                dwData["ABAMANDANT"] = MANDANT;
                dwData["EXART"] = EXART;
                Result = INTEXDBC.QueryABFOLGE(dwData, 8);
                Return.Merge(Result);
                dwData["ABAMANDANT"] = MANDANT;
                dwData["EXART"] = EXART;
                Result = INTEXDBC.QueryKSLEIST(dwData, 5);
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public void DELETE_ORDER_NETWORK(string krs, string lfd, string pos, string company = "DST", string ordertype = "V")
        { 
            try
            {
                if (ordertype == "K")
                {
                    INTEXDBC.DeleteABPOS("00", krs, lfd, pos, 3);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}